package Game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Card {

	/*ADUFOLSO = new Point(20,9);
	ADUKILENC = new Point(14,8);
	ASZ = new Point(11,7);
	CSIKO = new Point(4,5);
	FOLSO = new Point(3,4);
	ALSO = new Point(2,3);
	TIZ = new Point(10,6);
	KILENC = new Point(0,2);
	NYOLC = new Point(0,1);
	HET = new Point(0,0);*/
	
	private final static int WIDTH = 96;
	private final static int HEIGHT = 151;
	
	private int kindOf;
	private int color;
	private BufferedImage image;
	private Image cardImage;
	private int posX;
	private int posY;
	private boolean actClick = false;
	private boolean taked = false;
	private static int s = 0;
	private boolean forTake = false;
	private int value = 0;
	
	public Card(int color, int kindOf){
		this.color = color;
		this.kindOf = kindOf;
		makeImage();
		setValue();
	}
	
	private void toAdu(){
		if(kindOf == 5){
			value = 20;
		}
		if(kindOf == 2){
			value = 14;
		}
	}
	
	private void setValue(){
		switch(kindOf){
			case 7:
				value = 11;
			case 6:
				value = 4;
			case 5:
				value = 3;
			case 4:
				value = 2;
			case 3:
				value = 10;
			default:
				value = 0;
		}
	}
	
	private void makeImage(){
		try {
			image = ImageIO.read(new File("images\\magyar_kartya.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		cardImage = image.getSubimage(kindOf*WIDTH, color*HEIGHT, WIDTH, HEIGHT);
	}
	
	public void draw(Graphics g){
		g.drawImage(cardImage, posX, posY, null);
	}
	
	public Rectangle getBounds(){
		if(!forTake){
			return new Rectangle(posX, posY, (WIDTH/2), HEIGHT);			
		}
		else{
			return new Rectangle(posX, posY, WIDTH, HEIGHT);
		}
	}
	
	public void setPos(int x, int y){
		posX = x;
		posY = y;
	}
	
	public void mouseClicked(MouseEvent arg0){
		if(getBounds().contains(arg0.getPoint())){
			if(!actClick && !taked){
				posY -= 20;
				actClick = true;
			}
			else{
				if(!taked){
					take();
					Deck.centerDeck.add(this);
					taked = true;
				}
			}
		}
		else{
			if(actClick && !taked){
				posY += 20;
				actClick = false;
			}
		}
	}

	private void take() {
		posX = 275 + s*WIDTH;
		posY = 250;
		s++;
		
	}
	
	public boolean isTaked() {
		return taked;
	}

	public void setForTake(boolean forTake) {
		this.forTake = forTake;
		
	}
	
	public int getKindOf() {
		return kindOf;
	}
	
	public int getColor() {
		return color;
	}
	
	public int getValue() {
		return value;
	}
}
